/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = frcmd_http.h                                       */
/*  NOTE      =                                                    */
/*  DATE      = 2017/02/06 by yaohl                                */

#include <stdio.h>

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <ctype.h>
#include <strings.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <json-c/json.h>

#include <exception>
using namespace std;

#include "frcomm.h"
#include "frlog.h"
#include "frmem.h"
#include "frame_params.h"
#include "frstat_system.h"

#define SERVER_STRING "Server: apm_httpd/1.0\r\n"

/**********************************************************************/
/* Return the informational HTTP headers about a file. */
/* Parameters: the socket to print the headers on
 *             the name of the file */
/**********************************************************************/
void frcmd_http_frameInfo(int i_socket_client)
{
    char arr_buff[1024];
    const char *p_msg=NULL;
    json_object *p_st_json= NULL;
    unsigned int ui_body_len=0;

    /* 正常的 HTTP header */
    strcpy(arr_buff, "HTTP/1.0 200 OK\r\n");
    /* 服务器信息*/
    strcat(arr_buff, SERVER_STRING);
    strcat(arr_buff, "Content-Type: text/html;charset=UTF-8\r\n");
    strcat(arr_buff, "Connection: close\r\n");

    // 框架信息
    p_st_json = json_object_new_object();
    if(p_st_json == NULL)
        return;

    try
    {
        frstat_system_info(p_st_json);
        p_msg= json_object_to_json_string_ext(p_st_json, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED);
        if(p_msg != NULL)
            ui_body_len = strlen(p_msg);
        if(ui_body_len > 0)
        {
            char arr_len[20];
            snprintf(arr_len, sizeof(arr_len)-1, "%d", ui_body_len);
            strcat(arr_buff, "Content-Length: ");
            strcat(arr_buff, arr_len);
            strcat(arr_buff, "\r\n");
        }
        strcat(arr_buff, "\r\n");
        send(i_socket_client, arr_buff, strlen(arr_buff), 0);

        if(p_msg != NULL)
            send(i_socket_client, p_msg, strlen(p_msg), 0);
    }
    catch (exception& e)
    {
    }
    json_object_put(p_st_json);

    return;
}

/**********************************************************************/
/* This function starts the process of listening for web connections
 * on a specified port.  If the port is 0, then dynamically allocate a
 * port and modify the original port variable to reflect the actual
 * port.
 * Parameters: pointer to variable containing the port to connect on
 * Returns: the socket */
/**********************************************************************/
int frcmd_http_startup(u_short ui_port)
{
    int i_socket_http = 0;
    int i_opt=1;
    struct linger so_linger;
    struct sockaddr_in server_address;

    /* 建立 socket */
    i_socket_http = socket(PF_INET, SOCK_STREAM, 0);
    if (i_socket_http == -1)
    {
        FRLOG_ERROR_DETAIL("socket error");
        return 0;
    }

    memset(&server_address, 0, sizeof(server_address));
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(ui_port);
    server_address.sin_addr.s_addr = htonl(INADDR_ANY);

    if(setsockopt(i_socket_http,SOL_SOCKET,SO_REUSEADDR,&i_opt,sizeof(i_opt))<0)
        FRLOG_INFO_DETAIL("setsockopt failed");

//    so_linger.l_onoff = 1;
//    so_linger.l_linger = 0;
//    setsockopt(i_socket_http, SOL_SOCKET, SO_LINGER, &so_linger, sizeof(so_linger));

    if (bind(i_socket_http, (struct sockaddr *)&server_address, sizeof(server_address)) < 0)
    {
        FRLOG_ERROR_DETAIL("bind error");
        close(i_socket_http);
        return 0;
    }

    /* 开始监听*/
    if (listen(i_socket_http, 5) < 0)
    {
        FRLOG_ERROR_DETAIL("listen error");
        close(i_socket_http);
        return 0;
    }

    /* 返回 socket id */
    return i_socket_http;
}


